package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.DeviceFtService;
import com.eastjet.maintenance.domain.DeviceFt;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.bouncycastle.pqc.crypto.newhope.NHOtherInfoGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ft")
public class FTController {

    @Autowired
    private DeviceFtService deviceFtService;

    @RequestMapping("/list")
    public Page<DeviceFt> getFTList(
            @RequestBody Map<String, Object> map)
    {

        System.out.println("map:" + map);

        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            // 返回空的 Page<DeviceFt> 对象
            return  deviceFtService.getDeviceFtList(0, 0, "");
      }
        Integer page = (Integer) map.get("page");
        Integer size = (Integer) map.get("size");
        String condition = (String) map.get("condition");

        System.out.println("condition:" + condition);
        System.out.println("page:" + page);
        System.out.println("size:" + size);
        return  deviceFtService.getDeviceFtList(page, size, condition);
    }


    @RequestMapping("/add")
    public String addFT(@RequestBody Map<String, Object> map) {
        System.out.println("addmap:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceFt deviceFt = new DeviceFt();
        System.out.println("addmap:" + map);
        if (map.get("id") == null || map.get("id") == "请选择" || map.get("id") == "")
        {
            deviceFt.setFtLevel(0);
            deviceFt.setFtFather(0);
        }else
        {
            deviceFt.setFtLevel((Integer) map.get("id"));
        }

        deviceFt.setFtSort(  Integer.valueOf( map.get("sort").toString()));
        deviceFt.setFtName((String) map.get("name"));
        deviceFt.setCode((String) map.get("code"));
        deviceFt.setFtLevel(  Integer.valueOf( map.get("sort").toString()));
        deviceFt.setFtState( Integer.valueOf( map.get("state").toString()));
        deviceFt.setFtNotes((String) map.get("notes"));


        return deviceFtService.updateDeviceFt(deviceFt);

    }


    @RequestMapping("/update")
    public String updateFT( @RequestBody  Map<String, Object> map) {
        System.out.println("upmap:" + map);
        DeviceFt deviceFt = new DeviceFt();
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        if (map.get("id") == null || "请选择".equals(map.get("id")) || map.get("id") == "")
        {
            deviceFt.setFtFather(0);
        }else
        {
            deviceFt.setFtFather((Integer) map.get("id"));
        }

        deviceFt.setFtSort((Integer) map.get("sort"));
        deviceFt.setFtName((String) map.get("name"));
        deviceFt.setCode((String) map.get("code"));
        deviceFt.setFtLevel((Integer) map.get("sort"));
        deviceFt.setFtState((Integer) map.get("state"));
        deviceFt.setFtNotes((String) map.get("notes"));
        deviceFt.setFtId((Integer) map.get("fid"));

        return deviceFtService.updateDeviceFt(deviceFt);

    }

    @RequestMapping("/delete")
    public String deleteFT( @RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);

        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }

        // 获取 state 值
        Integer state = (Integer) map.get("state");
        // 获取 ids 列表
        List<Integer> ids = (List<Integer>) map.get("ids");

        // 创建 DeviceFt 对象并设置其状态
        DeviceFt deviceFt = new DeviceFt();
        deviceFt.setIsDelete(1);

        // 打印获取到的 state 值
        System.out.println("state: " + state);

        // 循环打印 ids 列表中的每个 ID
        if (ids != null) {
            for (Integer id : ids) {
                System.out.println("id: " + id);
                deviceFt.setFtId(id);
                deviceFtService.deleteDeviceFt(deviceFt);
            }
            return "success";
        } else {
            System.out.println("ids 列表为空或未提供");
            return "参数错误";
        }
    }




    @RequestMapping("/updateState")
    public String updateStateFT(@RequestBody Map<String, Object> map) {
        System.out.println("map:" + map);
        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }

        // 获取 state 值
        Integer state = (Integer) map.get("state");
        // 获取 ids 列表
        List<Integer> ids = (List<Integer>) map.get("ids");

        // 创建 DeviceFt 对象并设置其状态
        DeviceFt deviceFt = new DeviceFt();
        deviceFt.setFtState(state);

        // 打印获取到的 state 值
        System.out.println("state: " + state);

        // 循环打印 ids 列表中的每个 ID
        if (ids != null) {
            for (Integer id : ids) {
                System.out.println("id: " + id);
                deviceFt.setFtId(id);
                deviceFtService.updateStateDeviceFt(deviceFt);
            }
            return "success";
        } else {
            System.out.println("ids 列表为空或未提供");
            return "参数错误";
        }
    }
}
